
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "uart.h"

typedef struct wifi_sihaizhilian_data
{
    char data_cache[1500];
    char data_current[1500];
    int len_cache;
    int len_current;
} wifi_data_t;

static wifi_data_t wifi_data = {{0}, {0}, 0, 0};

void read_callback(char *data, int len)
{
    // printf("%d\n%s\n", len, data);
    int flag = 0;

    int base = len + wifi_data.len_cache;

    if (base % 50 == 0)
    {
        if (wifi_data.len_cache != 0)
        {
            memcpy(wifi_data.data_current, wifi_data.data_cache, wifi_data.len_cache);
            wifi_data.len_current = wifi_data.len_cache;
            wifi_data.len_cache = 0;
        }

        memcpy(wifi_data.data_current + wifi_data.len_current, data, len);
        wifi_data.len_current += len;

        flag = 1;
    }
    else
    {
        memcpy(wifi_data.data_cache, data, len);
        wifi_data.len_cache = len;

        flag = 0;
    }

    if (flag)
    {
        printf("%d\n%s\n", base, wifi_data.data_current);

        memset(wifi_data.data_current, 0, 1500);
        wifi_data.len_current = 0;
    }
}

void read_callback2(char *data, int len)
{
    int flag = 0;

    int base = len + wifi_data.len_cache;

    if (base % 50 == 0)
    {
        if (wifi_data.len_cache != 0)
        {
            memcpy(wifi_data.data_current, wifi_data.data_cache, wifi_data.len_cache);
            wifi_data.len_current = wifi_data.len_cache;
            wifi_data.len_cache = 0;
        }

        memcpy(wifi_data.data_current + wifi_data.len_current, data, len);
        wifi_data.len_current += len;

        flag = 1;
    }
    else
    {

        if (wifi_data.len_cache >= 1000)
        {
            memcpy(wifi_data.data_current, wifi_data.data_cache, 1000);
            wifi_data.len_current = 1000;

            int remain = wifi_data.len_cache - 1000;
            char tmp[500] = {0};
            memcpy(tmp, wifi_data.data_cache + 1000, remain);
            memcpy(wifi_data.data_cache,tmp,remain);
            wifi_data.len_cache = remain;

            flag = 1;
        }
        else
        {
            memcpy(wifi_data.data_cache + wifi_data.len_cache, data, len);
            wifi_data.len_cache += len;

            flag = 0;
        }
    }

    if (flag)
    {
        printf("%d\n%s\n", wifi_data.len_current, wifi_data.data_current);

        memset(wifi_data.data_current, 0, 1500);
        wifi_data.len_current = 0;
    }
}

#define uart_dev "/dev/ttyS1"
int main()
{
    UART_T uart_handle;
    int res = uart_open(uart_dev, &uart_handle);
    if (res != 0)
    {
        printf("uart open error!\n");
        return -1;
    }

    uart_set(115200, 8, 'N', 1, &uart_handle);
    uart_read_register(read_callback2, &uart_handle);

    uart_read_event_start(&uart_handle);
#if 0
    UART_T wuart_handle;
    res = uart_open("/dev/ttyUSB1", &wuart_handle);
    if (res != 0)
    {
        printf("uart open error!\n");
        return -1;
    }

    uart_set(115200, 8, 'N', 1, &wuart_handle);
#endif
    while (1)
    {

        // uart_send(&wuart_handle,"HelloWorld\r\n",12);

        sleep(3);
    }
    return 0;
}